package core
{
	import flash.display.Sprite;
	import flash.net.URLRequest;
	import flash.system.Security;
	
	import org.ais.system.Ais;

	public class Main extends Sprite
	{
		private var _url:String;
		private var _p:String;
		
		public function Main()
		{
			try {
				Security.allowDomain("*");
				Security.allowInsecureDomain("*");
			}
			catch (error:*) {}
		}
		
		public function connect(im:*):void
		{
			_p = loaderInfo.parameters["p"];
			_url = loaderInfo.parameters["url"];
			if (!_p && !_url) {
				clearPlugin();
				return;
			}
			if (Ais.IMain.Swf.hasKey("UNETSTREAMY_IMOON") !== -1) {
				Ais.IMain.Swf.get("UNETSTREAMY_IMOON", "swf").content.getNetStream().setCheckURL(__checkURLF);
			}
			im = null;
		}
		
		private function __checkURLF(request:Object, type:int = 0, p:uint = 0):URLRequest
		{
			var r:URLRequest;
			if (request is String) r = new URLRequest(request as String);
			else r = request as URLRequest;
			if (r.url) {
				r.url = r.url.replace(/start\=0/ig, "");
			}
			if (type === 0) {
				var _r:RegExp = /(http|ftp)/ig;
				if (_r.test(r.url) === true) r.url = _url + r.url;
				_r = null;
			}
			else if (type === 1) {
				r.url += _p + p;
			}
			request = null;
			return r;
		}
		
		public function clearPlugin():void
		{
			Ais.IMain.Swf.remove(Ais.IMain.Swf.hasSwf("swf", parent));
		}
		
		public function clear():void
		{
			if (Ais.IMain.Swf.hasKey("UNETSTREAMY_IMOON") !== -1) Ais.IMain.Swf.get("UNETSTREAMY_IMOON", "swf").content.getNetStream().setCheckURL();
			_p = null;
			_url = null;
		}
		
	}
}